﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CP.Entities;
using CP.DBService;
using CP.Utility;

namespace CP.Repositories.GPM
{
    public class ModuleRepository
    {
        DBHelper db = DBHelper.GPMCreateInstance();

        public IList<Module> GetAllModules()
        {
            return db.ExecuteObject<Module>("select * from Module order by module_id");
        }

        public Module GetModuleByID(int moduleid)
        {
            IList<Module> Modules = db.ExecuteObject<Module>(string.Format(@"select * 
                                                from Module
                                        where module_id ='{0}' ", moduleid));
            if (Modules.Count > 0)
                return Modules[0];
            return null;
        }

        public Module GetModuleByAssemblyName(string assemblyname)
        {
            return db.ExecuteSingleObject<Module>(string.Format(@"select * from module 
                                        where assembly_name = '{0}'", assemblyname));
        }

        public int GetModuleIDBySeq()
        {
            return SequenceGenerator.GetSeqNextValue("SEQ_MODULE");
        }

        public bool DeleteModule(IList<int> ids)
        {
            try
            {
                db.BeginTransaction();
                foreach (int id in ids)
                {
                    db.ExecuteNonQuery(string.Format(@"DELETE   webpart_right_button t1
                                         WHERE   EXISTS (SELECT   NULL
                                                           FROM   webpart t2
                                                          WHERE   t1.webpart_id = t2.webpart_id 
                                                                AND t2.module_id = {0})", id));
                    db.ExecuteNonQuery(string.Format(@"DELETE   WEBPART_RIGHT_LIST t1
                                         WHERE   EXISTS (SELECT   NULL
                                                           FROM   webpart t2
                                                          WHERE   t1.webpart_id = t2.webpart_id 
                                                                AND t2.module_id = {0})", id));
                    db.ExecuteNonQuery(string.Format(@"DELETE   button_list t1
                                         WHERE   EXISTS (SELECT   NULL
                                                           FROM   webpart t2
                                                          WHERE   t1.webpart_id = t2.webpart_id 
                                                                AND t2.module_id = {0})", id));
                    db.ExecuteNonQuery(string.Format(@"DELETE   column_list t1
                                         WHERE   EXISTS (SELECT   NULL
                                                           FROM   webpart t2
                                                          WHERE   t1.webpart_id = t2.webpart_id 
                                                                AND t2.module_id = {0})", id));
                    db.ExecuteNonQuery(string.Format(@"delete WEBPART 
                                        where Module_id = {0} ", id));
                    db.ExecuteNonQuery(string.Format(@"delete Module 
                                        where Module_id = {0} ", id));

                }
                db.Commit();
                return true;
            }

            catch (Exception ex)
            {
                db.Rollback();
                throw ex;
            }
        }

        public bool AddModule(Module module)
        {
            db.ExecuteNonQuery(string.Format(@"insert into Module(Module_id,Module_name,
                                ASSEMBLY_NAME,NAME_SPACE,REMARK,DB_TYPE) 
                                        values({0},'{1}','{2}','{3}','{4}',2) ",
                                module.module_id, module.module_name, module.assembly_name,
                                module.name_space, module.remark));
            return true;
        }

        public bool UpdateModule(Module module)
        {
            db.ExecuteNonQuery(string.Format(@"update Module set  Module_name='{1}',
                                ASSEMBLY_NAME= '{2}',NAME_SPACE='{3}',REMARK='{4}'  
                               where Module_id = {0} ",
                               module.module_id, module.module_name, module.assembly_name,
                               module.name_space, module.remark));
            return true;
        }
    }
}
